/*
 * Virtual Earth Screensaver
 * Keith Kinnan
 * 5/12/2007
 * 9/30/2006
 * 
 * Portions of this code is based on the Visual Studio 2005 Starter Kit RSS Screen Saver:
 * http://msdn.microsoft.com/vstudio/express/visualcsharp/starterkit/default.aspx#rssss
 */

using System.Xml;

namespace VirtualEarthScreensaver
{
    /// <summary>
    /// Representation of an Item element in an RSS 2.0 XML document.
    /// Zero or more RssItems are contained in an RssChannel.
    /// </summary>
    public class RssItem 
    {
        private readonly string title;
        private readonly string description;
        private readonly string link;
        private readonly string content;
        private readonly string geoRssLat;
        private readonly string geoRssLon;

        public string Title { get { return title; } }
        public string Description { get { return description; } }
        public string Link { get { return link; } }
        public string Content { get { return content; } }
        public string Latitude { get { return geoRssLat; } }
        public string Longitude { get { return geoRssLon; } }


        /// <summary>
        /// Create an RSS Item from strings
        /// </summary>
        /// <param name="strTitle"></param>
        /// <param name="strDescription"></param>
        /// <param name="strLink"></param>
        /// <param name="strContent"></param>
        /// <param name="strLatitude"></param>
        /// <param name="strLongitude"></param>
        internal RssItem(string strTitle, string strDescription, string strLink, string strContent, string strLatitude, string strLongitude)
        {
            title = strTitle;
            description = strDescription;
            link = strLink;
            content = strContent;
            geoRssLat = strLatitude;
            geoRssLon = strLongitude;
        }

        /// <summary>
        /// Build an RSSItem from an XmlNode representing an Item element inside an RSS 2.0 XML document.
        /// </summary>
        /// <param name="itemNode"></param>
        internal RssItem(XmlNode itemNode)
        {
            XmlNode selected;
            selected = itemNode.SelectSingleNode(Properties.Resources.RssTitle);
            if (selected != null)
            {
                title = selected.InnerText;
            }

            selected = itemNode.SelectSingleNode(Properties.Resources.RssDescription);
            if (selected != null)
            {
                description = selected.InnerText;
            }

            selected = itemNode.SelectSingleNode(Properties.Resources.RssLink);
            if (selected != null)
            {
                link = selected.InnerText;
            }
                
            XmlNamespaceManager xmlNSManager= new XmlNamespaceManager(new System.Xml.NameTable());
            xmlNSManager.AddNamespace(Properties.Resources.RssContent, Properties.Resources.RssContentUrl);
            selected = itemNode.SelectSingleNode(Properties.Resources.RssContentEncoded, xmlNSManager);
            if (selected != null)
            {
                content = selected.InnerText;
            }
                
            xmlNSManager = new XmlNamespaceManager(new System.Xml.NameTable());
            xmlNSManager.AddNamespace(Properties.Resources.GeoRss, Properties.Resources.GeoRssUrl);
            selected = itemNode.SelectSingleNode(Properties.Resources.GeoRssPoint, xmlNSManager);
            if (selected != null)
            {
                string latLongStr = selected.InnerText;
                char[] delimiters = { ' ', ',' };
                string[] latLongPair = latLongStr.Split(delimiters);
                if (latLongPair.Length == 2)
                {
                    geoRssLat = latLongPair[0];
                    geoRssLon = latLongPair[1];
                }
            }
        }
    }
}